﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Security.Cryptography;

namespace tracker.Models
{
    public class Utilities
    {
        /// <summary>
        /// Mã hóa 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string password = s.ToString();
            return password;
        }
        public static string getJSONString(DataSet ds)
        {
            StringBuilder sb = new StringBuilder();
            
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                {
                    //sb.Append(ds.Tables[0].Columns[j].ColumnName +":"+
//                    ds.Tables[0].Columns[j].DataType.f
                    
                }
            }
            return "";
        }
        public static string getJsonString<T>(List<T> objectData, string dataString, int intType)
        {
            JavaScriptSerializer javaser = new JavaScriptSerializer();
            StringBuilder builder = new StringBuilder();
            if (intType == 1)
            {
                if (dataString.Length > 0)
                {
                    builder.Append("{");
                    builder.Append(dataString);
                    builder.Append(javaser.Serialize(objectData));
                    builder.Append("}");
                    return builder.ToString();
                }
                else
                {
                    return "[]";
                }
            }
            else if (intType == 2)
            {
                return javaser.Serialize(objectData);
            }
            return "[]";
        }
        public static bool isDateTime(string __data)
        {
            DateTime dtcheck;
            return DateTime.TryParse(__data, out dtcheck);
        }
        public static bool isInt(string __data)
        {
            int icheck = 0;
            return int.TryParse(__data, out icheck);
        }

        // Get first character of USER_SECUTIRY_CODE from session
        public static String getCurrentUserType(Controller c){
            try
            {
                return c.Session[Common.USER_SECUTIRY_CODE].ToString().Substring(0, 1).ToUpper();
            }
            catch
            {
                return "";
            }
        }

        // Create JSON string
        public static string createJSON(string attribute, string value, bool noDoubleQuote = false)
        {
            string result = "";
            result += "{" + attribute + ":";
            if (noDoubleQuote)
            {
                result += value;
            }
            else
            {
                result += "\"" + value + "\"";
            }
            result += "}";
            return result;
        }
        public static bool regexCheck(string pattern, string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                return false;
            }
            Regex rgx = new Regex(pattern);
            return rgx.IsMatch(value);
        }
        public static bool emailValid(string emailaddress)
        {
            try
            {
                MailAddress m = new MailAddress(emailaddress);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }
        public static string createSecurityCode(string input)
        {
            string salt = "haule.cntt@gmail.com";
            return GetMD5Hash(input + salt);
        }

        // Check dataset has data
        public static bool hasRow(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0].Rows.Count > 0;
            }
            return false;
        }
        public static String getAccountStatus(int id)
        {
            switch (id)
            {
                case 0:return "_TrackerMainUserDevice_No";
                case 1: return "_TrackerMainUserDevice_No";
                case -1: return "_TrackerMain_Lock";
            }

            return "null";
        }
        public static String getPermission(int id)
        {
            switch (id)
            {
                case 1: return "_NewForText_NormalUser";
                case 2: return "_NewForText_Administrator";
                //case -1: return "_TrackerMain_Lock";
            }
            return "null";
        }
    }

}
